package com.example.control.regisControl;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Controller
public class ImageCode{

    @RequestMapping("/regis/get_image_code")
    public void getImageCode(HttpServletRequest request,HttpServletResponse response) {
        Random random = new Random();
        int width=100,height=35;
        int Line=4+random.nextInt(6);
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
        Graphics gs = image.getGraphics();

        gs.setColor(getRandColor(random));

        gs.fillRect(0,0,width,height);
        gs.setColor(getRandColor(random));
        int x=random.nextInt(4),y=0;
        int x1=width-random.nextInt(4),y1=0;
        while(Line-->0){
            y=random.nextInt(height-random.nextInt(4));
            y1=random.nextInt(height-random.nextInt(4));
            gs.drawLine(x,y,x1,y1);

        }
        int fsize = (int)(height*0.8);
        int fx=0,fy=fsize;
        gs.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,fsize));
        String codeText="";
        String rand="123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        gs.setColor(getBackColor(random));
        for(int i=1;i<=4;i++){
            String c = String.valueOf(rand.charAt(random.nextInt(rand.length())));
            codeText+=c;
            fy=(int)((0.9-random.nextInt(10)*0.03)*height);
            gs.drawString(c,fx,fy);
            fx+=(width / 4) * (Math.random() * 0.3 + 0.8);
        }
        //扭曲图片
        //shearX(gs, width, height,getRandColor(random) );
        //shearY(gs, width, height, getRandColor(random));
        float yawpRate = 0.05f;// 噪声率
        int area = (int) (yawpRate * width * height);//噪点数量
        for (int i = 0; i < area; i++) {
            int xxx = random.nextInt(width);
            int yyy = random.nextInt(height);
            int rgb = getRandColor(random).getRGB();
            image.setRGB(xxx, yyy, rgb);
        }
        gs.dispose();
        HttpSession session = request.getSession();
        session.setAttribute("codeText",codeText);


        try {
            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(image,"jpg",outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void shearX(Graphics g, int w1, int h1, Color color) {
        Random random=new Random();
        int period = 2;

        boolean borderGap = true;
        int frames = 1;
        int phase = random.nextInt(2);

        for (int i = 0; i < h1; i++) {
            double d = (double) (period >> 1)* Math.sin((double) i / (double) period
                    + (2.2831853071795862D * (double) phase)/ (double) frames);
            g.copyArea(0, i, w1, 1, (int) d, 0);
            if (borderGap) {
                g.setColor(color);
                g.drawLine((int) d, i, 0, i);
                g.drawLine((int) d + w1, i, w1, i);
            }
        }

    }

    private static void shearY(Graphics g, int w1, int h1, Color color) {
        Random random=new Random();
        int period = random.nextInt(40) + 10; // 50;

        boolean borderGap = true;
        int frames = 20;
        int phase = random.nextInt(2);
        for (int i = 0; i < w1; i++) {
            double d = (double) (period >> 1)
                    * Math.sin((double) i / (double) period
                    + (2.2831853071795862D * (double) phase)/ (double) frames);
            g.copyArea(i, 0, 1, h1, 0, (int) d);
            if (borderGap) {
                g.setColor(color);
                g.drawLine(i, (int) d, i, 0);
                g.drawLine(i, (int) d + h1, i, h1);
            }

        }

    }

    private Color getRandColor(Random random) {
        return new Color(100+random.nextInt(115),100+random.nextInt(115),100+random.nextInt(115));
    }

    private Color getBackColor(Random random) {
        return new Color(random.nextInt(55),random.nextInt(55),random.nextInt(55));
    }
}
